﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;

namespace Nirvana
{
    [AddComponentMenu("Nirvana/UI/Language/Object I18N")]
    [DisallowMultipleComponent]
    [ExecuteInEditMode]
    public sealed class ObjectI18N : Translatable
    {
        [Serializable]
        private struct BindLanguageObject
        {
            [Tooltip("The languaget type.")]
            public I18NLanguageType languagetype;
            [Tooltip("The translate Object.")]
            public GameObject translate;
        };

        /// <summary>
        /// 内置翻译表，不通过keyid获取，直接设置
        /// </summary>
        [SerializeField]
        [Tooltip("The bind Language Object list.")]
        private List<BindLanguageObject> binds = new List<BindLanguageObject>();
        private GameObject curSelectObj;

        protected override void TranslateText()
        {
            this.GetTranslate();
        }

        private  void GetTranslate()
        {
            curSelectObj = null;
            I18NLanguageType language = I18N.Language;
            foreach (var f in binds)
            {
                if (f.translate != null)
                {
                    bool isSelect = f.languagetype == language;
                    if (isSelect) curSelectObj = f.translate;
                    f.translate.SetActive(isSelect);
                    //都指向同一对象
                    if (!isSelect && f.translate == curSelectObj)
                    {
                        f.translate.SetActive(true);
                    }
                }
                
            }

        }

    }
}

